<template>
  <!--回到顶部-->
  <transition
    enter-active-class="w-animated w-bounceInRight"
    leave-active-class="w-animated w-bounceOutRight">
    <div
      v-if="backTop > 100"
      class="back-top w-round w-flex-center"
      @click="backTopClick">
      <i class="el-icon-arrow-up"/>
    </div>
  </transition>
</template>

<script>
  import {ScrollTop} from 'yb-tool'

  /**
   * 返回到顶部
   */
  export default {
    name: 'BackTop',
    data() {
      return {
        /**
         * 距离顶部的距离
         */
        backTop: 0
      }
    },
    mounted() {
      window.addEventListener ('scroll', this.handleScroll, false);
      window.addEventListener ('resize', this.handleScroll, false);
    },
    beforeDestroy() {
      window.removeEventListener ('scroll', this.handleScroll, false);
      window.removeEventListener ('resize', this.handleScroll, false);
    },
    methods: {
      /**
       * 返回顶部按钮点击
       */
      backTopClick() {
        const sTop = document.documentElement.scrollTop || document.body.scrollTop;
        ScrollTop (window, sTop, 0, 800);
      },
      /**
       * 监听网站滚动，显示向上箭头
       */
      handleScroll() {
        this.backTop = document.documentElement.scrollTop || document.body.scrollTop;
      }
    }
  }
</script>

<style scoped>
  .back-top {
    position: fixed;
    right: 30px;
    bottom: 30px;
    width: 40px;
    height: 40px;
    background-color: rgba(0, 0, 0, .5);
    color: #eee;
    font-size: 24px;
  }
</style>
